library(survey)
library(dplyr)
library(stargazer)
library(performance)

# Chapter 3

## Model for Table 3.1 
# model: multivariate model based on demographics, logit specification, DV: Q8_d 
# Q18a for "looks like me"

summary(factor(mil_conf.df$Q18))
sum(summary(factor(mil_conf.df$Q18)))-23
mil_conf.df2 <- mil_conf.df[mil_conf.df$Q18 < 98,]
length(mil_conf.df2$Q18)
summary(factor(mil_conf.df2$Q18))

# Q18 - "Looks like me" - 1 for most and some look like me
mil_conf.df2$Q18a <- 0
mil_conf.df2$Q18a[mil_conf.df2$Q18 < 3] <- 1
colnames(mil_conf.df2)

# Create race binary variables
mil_conf.df2$white <- 0
mil_conf.df2$white[mil_conf.df2$RACETHNICITY == 1] <- 1

mil_conf.df2$asian <- 0
mil_conf.df2$asian[mil_conf.df2$RACETHNICITY == 6] <- 1

# Create educ5
summary(factor(mil_conf.df2$EDUC))
mil_conf.df2$educ5[mil_conf.df2$EDUC < 9] <- 1
mil_conf.df2$educ5[mil_conf.df2$EDUC == 9] <- 2
mil_conf.df2$educ5[mil_conf.df2$EDUC ==10 | mil_conf.df2$EDUC == 11] <- 3
mil_conf.df2$educ5[mil_conf.df2$EDUC == 12] <- 4
mil_conf.df2$educ5[mil_conf.df2$EDUC > 12] <- 5

# Create silent
mil_conf.df2$silent <- 0
mil_conf.df2$silent[mil_conf.df2$generation==1] <- 1

## Create weighted survey design object
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df2
  )

summary(factor(w1_design$variables$Q8_d))
summary(factor(w1_design$variables$Q18a))

fig3.1_mod <- svyglm(Q8_d ~ Q18a + dem + rep + ideo3 + male + white + black + hispanic + asian + income5 +
                       educ5 + unemployed + silent + boomer + genx + millen + activeduty + vet +
                       social + family + midwest + south + west + catholic + christian + norelig +
                       city + rural + married + A_2 + A_3 + A_4 + A_5 + A_6 + A_7 + A_8,
                     family = binomial(link = "logit"),
                     design = w1_design)
summary(fig3.1_mod)

stargazer(fig3.1_mod)
check_collinearity(fig3.1_mod)


##################################################################


## Model for Table 3.2 
# model: multivariate model based on demographics, logit specification, DV: Q8_d

# Knowledge
summary(factor(mil_conf.df$Knowledge))
mil_conf.df$k2 <- mil_conf.df$Knowledge/6


## Create weighted survey design object
w1_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )

fig3.2_mod <- svyglm(Q8_d ~ k2 + Q18a + dem + rep + ideo3 + male + white + black + hispanic + asian + 
                       income5 + educ5 + unemployed + married + 
                       silent + boomer + genx + millen + 
                       activeduty + vet + social + family + 
                       midwest + south + west + 
                       catholic + christian + norelig +
                       city + rural + A_2 + A_3 + A_4 + A_5 + A_6 + A_7 + A_8,
                     family = binomial(link = "logit"),
                     design = w1_design)
summary(fig3.2_mod)

stargazer(fig3.2_mod)
check_collinearity(fig3.2_mod)


#################################################################


## Model for Table 3.3 
# model: multivariate model based on demographics, logit specification, DV: Q11_d

summary(factor(df$Q11))
summary(factor(df$Q11_d))


## Create weighted survey design object
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

## creating Model for Table 3.3 
# model: multivariate model based on demographics, logit specification, DV: Q11_d
fig3.3_mod <- svyglm(Q11_d ~ dem + rep + ideo3 + 
                       male + white + black + hispanic + asian + 
                       income5 + dem*EDUC5 + rep*EDUC5 + unemployed + married + 
                       silent + boomer + genx + millen + 
                       activeduty + vet + social + family + 
                       midwest + south + west + 
                       catholic + christian + norelig +
                       city + rural + 
                       A2 + A3 + A4 + A5 + A6 + A7 + A8,
                     family = binomial(link = "logit"),
                     design = w2_design)

summary(fig3.3_mod)
check_collinearity(fig3.3_mod)
stargazer(fig3.3_mod)


##################################################################


## Model for Table 3.4 
# model: multivariate model based on demographics, logit specification, DV: Q17a

summary(factor(df$Q17A))
df$Q17A_REP <- NA
summary(df$Q17A_REP)
df$Q17A_REP[df$Q17A == 1 | df$Q17A == 3] <- 0
df$Q17A_REP[df$Q17A == 2] <- 1

summary(df$Q17A_REP)
summary(factor(df$Q17A_REP))
summary(df$weight2)

## Create weighted survey design object
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df
  )

## creating Model for Table 3.4
# model: multivariate model based on demographics, logit specification, DV: 
fig3.4_mod <- svyglm(Q17A_REP ~ dem + rep + ideo3 + 
                       male + white + black + hispanic + asian + 
                       income5 + dem*EDUC5 + rep*EDUC5 + unemployed + married + 
                       silent + boomer + genx + millen + 
                       activeduty + vet + social + family + 
                       midwest + south + west + 
                       catholic + christian + norelig +
                       city + rural + 
                       A2 + A3 + A4 + A5 + A6 + A7 + A8,
                     family = binomial(link = "logit"),
                     design = w2_design)

summary(fig3.4_mod)
check_collinearity(fig3.4_mod)
stargazer(fig3.4_mod)


##################################################################


## Model for Table 3.9
# model: multivariate model based on demographics, logit specification, DV: Q11_d

summary(factor(df$Q11))
summary(factor(df$Q11_d))
summary(factor(df$Q33))

# Recode attention to military news
df$Q33[df$Q33 == 1] <- 7
df$Q33[df$Q33 == 2] <- 6
df$Q33[df$Q33 == 3] <- 5
df$Q33 <- df$Q33 - 3
summary(factor(df$Q33))

## Create weighted survey design object
w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df[df$Q33<5,]
  )

summary(factor(w2_design$variables$Q33))

## creating Model for Table 3.9
# model: multivariate model based on demographics, logit specification, DV: Q11_d
fig3.9_mod <- svyglm(Q11_d ~ Q33 + dem + rep + ideo3 + 
                       male + white + black + hispanic + asian + 
                       income5 + EDUC5 + unemployed + married + 
                       silent + boomer + genx + millen + 
                       activeduty + vet + social + family + 
                       midwest + south + west + 
                       catholic + christian + norelig +
                       city + rural + 
                       A2 + A3 + A4 + A5 + A6 + A7 + A8,
                     family = binomial(link = "logit"),
                     design = w2_design)

summary(fig3.9_mod)
check_collinearity(fig3.9_mod)
stargazer(fig3.9_mod)

